package service;

import com.mysql.cj.xdevapi.JsonArray;
import dao.*;
import entity.Address;
import entity.ShoppingList;
import entity.ShoppingListItem;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import utils.DateUtils;
import utils.DbUtils;

import java.util.Date;
import java.util.List;

/**
 * @author lishunlin110
 * @version 1.0
 * @description: 用户服务层接口实现
 * @date 2021/10/16 23:02
 */
public class UserServiceImpl implements UserService{
    @Override
    public JSONArray getShoppingList(String userId) {
        ShoppingListDao dao = new ShoppingListDaoImpl();
        JSONArray jsonArray = new JSONArray();
        try {
            DbUtils.begin();
            List<ShoppingList> list = dao.getShoppingList(userId);
            DbUtils.commit();
            for (ShoppingList s:list){
                JSONObject json = new JSONObject();
                json.put("id",s.getId());
                json.put("create_time", DateUtils.utilToStr(s.getCreate_time()));
                json.put("u_id",userId);
                json.put("content",getShoppingListItem(s.getId()));
                jsonArray.put(json);
            }
            return jsonArray;
        } catch (JSONException e) {
            DbUtils.rollback();
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public JSONArray getShoppingListItem(int shoppinglistId) {
        ShoppingListDao dao = new ShoppingListDaoImpl();
        JSONArray jsonArray = new JSONArray();
        List<ShoppingListItem> items = null;
        try {
            DbUtils.begin();
            items = dao.getShoppingListItem(shoppinglistId);
            DbUtils.commit();
            for (ShoppingListItem s:items){
                JSONObject json = new JSONObject();
                json.put("p_num",s.getP_num());
                json.put("price",s.getPrice());
                json.put("color",s.getColor());
                json.put("type",s.getType());
                json.put("count",s.getCount());
                json.put("pic_adr",s.getPic_adr());
                jsonArray.put(json);
            }
            return jsonArray;

        } catch (Exception e) {
            DbUtils.rollback();
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int deleteShoppingList(int id) {
        ShoppingListDao dao = new ShoppingListDaoImpl();
        return dao.deleteShoppingList(id);
    }

    @Override
    public int addShoppingList(String u_id,String content) {
        //先存入商品信息
        ShoppingList shoppingList = new ShoppingList(u_id,new Date());
        ShoppingListDao dao = new ShoppingListDaoImpl();
        int shoppinglistId = 0;
        try {
            DbUtils.begin();
            dao.addShoppingList(shoppingList);
            //获取成功存入的那一条购物清单的id
            shoppinglistId= dao.getShoppingListId(u_id);
            JSONArray jsonArray = new JSONArray(content);
            //根据这题条购物清单id存这条购物清单的条目
            for (int i = 0; i < jsonArray.length(); i++) {
                ShoppingListItem item = new ShoppingListItem();
                item.setShoppinglist_id(shoppinglistId);
                JSONObject json = jsonArray.getJSONObject(i);
                item.setP_num((String) json.get("p_num"));
                item.setPrice((String) json.get("price"));
                item.setCount((Integer) json.get("count"));
                item.setColor((String) json.get("color"));
                item.setType((String) json.get("type"));
                item.setPic_adr((String) json.get("pic_adr"));
                dao.addShoppingListItem(item);
            }
            DbUtils.commit();
        } catch (Exception e) {
            DbUtils.rollback();
            e.printStackTrace();
            return 0;
        }

        return 1;
    }

    @Override
    public JSONArray getAddress(String userId) {
        AddressDao dao = new AddressDaoImpl();
        List<Address> list;
        JSONArray jsonArray = new JSONArray();
        try {
            DbUtils.begin();
            list = dao.getAddress(userId);
            String str = list.toString();
            if(!"[]".equals(str)){
                for (Address s:list){
                    JSONObject jsonobj = new JSONObject();
                    jsonobj.put("id",s.getId());
                    jsonobj.put("u_id",s.getU_id());
                    jsonobj.put("create_time",s.getCreate_time());
                    jsonobj.put("province",s.getProvince());
                    jsonobj.put("city",s.getCity());
                    jsonobj.put("region",s.getRegion());
                    jsonobj.put("street",s.getStreet());
                    jsonobj.put("detailed_addr",s.getDetailed_addr());
                    jsonobj.put("phone_number",s.getPhone_number());
                    jsonArray.put(jsonobj);
                }
                return jsonArray;
            }
            DbUtils.commit();
        }catch (Exception e){
            DbUtils.rollback();
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int deleteAddress(int id) {
        AddressDao dao = new AddressDaoImpl();
        return dao.deleteAddress(id);
    }

    @Override
    public int addAddress(String u_id, String province, String city, String region, String street, String detailAddress, String phoneNum) {
        Address address = new Address(u_id,new Date(),province,city,region,street,detailAddress,phoneNum);
        AddressDao dao = new AddressDaoImpl();
        return dao.addAddress(address);
    }

    @Override
    public int updateAddress(int id, String province, String city, String region, String street, String detailAddress, String phoneNum) {
        Address address = new Address(id,province,city,region,street,detailAddress,phoneNum);
        AddressDao dao = new AddressDaoImpl();
        return dao.updateAddress(address);
    }


    public static void main(String[] args) {
        UserService service = new UserServiceImpl();
//        String content = "[{\"p_num\":\"shdj\",\"color\":\"蓝色\",\"price\":\"2222\",\"count\":2,\"type\":\"bike\"}," +
//                "{\"p_num\":\"shdj22\",\"color\":\"heise色\",\"price\":\"332\",\"count\":4,\"type\":\"bike\"}]";
//        service.addShoppingList("644",content);

        service.deleteShoppingList(11);
    }

}
